package com.ymatou.iapi.openapibiz.testcase;

import java.text.SimpleDateFormat;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.openapibiz.parameter.Application;
import com.ymatou.iapi.openapibiz.parameter.ApplicationReturn;
import com.ymatou.iapi.openapibiz.parameter.AuthorizeBean;
import com.ymatou.iapi.openapibiz.parameter.ListUnauthorizeAppsBean;
import com.ymatou.iapi.openapibiz.parameter.NewThirdPartyAppBean;
import com.ymatou.iapi.openapibiz.service.AuthorizeCall;
import com.ymatou.iapi.openapibiz.service.ListUnauthorizeAppsCall;
import com.ymatou.iapi.openapibiz.service.NewThirdPartyAppCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.OpenApiBizCallService;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*查询所有未授权的第三方应用接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ListUnauthorizeApps{
	private static ListUnauthorizeAppsBean listunauthorizeappsBean;
	private static ListUnauthorizeAppsCall listunauthorizeappsCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询所有未授权的第三方应用接口");
	}

	@Before
	public void caseUp() {
		listunauthorizeappsBean=new ListUnauthorizeAppsBean();
		listunauthorizeappsCall=new ListUnauthorizeAppsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(listunauthorizeappsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListUnauthorizeApps_001() {
		Logger.start(true,"查询所有未授权的第三方应用接口");
		try {
			long sellerId = Long.valueOf(EnvSetup.getData("openapi_sellerid"));
			
			listunauthorizeappsBean.setSellerId(sellerId);
			
			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();
			
//			NewThirdPartyAppBean newThirdPartyAppBean = OpenApiBizCallService.createNewThirdPartyAppBean();
//			NewThirdPartyAppCall newThirdPartyAppCall = OpenApiBizCallService.newthirdpartyappCall(newThirdPartyAppBean);
//	
//			AuthorizeBean authorizeBean = OpenApiBizCallService.createAuthorizeBean_THIRD_PARTY(newThirdPartyAppCall.getAppId());
//			AuthorizeCall authorizeCall = OpenApiBizCallService.authorizeCall(authorizeBean);
//
//			NewThirdPartyAppBean newThirdPartyAppBean2 = OpenApiBizCallService.createNewThirdPartyAppBean();
//			NewThirdPartyAppCall newThirdPartyAppCall2 = OpenApiBizCallService.newthirdpartyappCall(newThirdPartyAppBean2);
//
//			listunauthorizeappsBean.setSellerId((long)authorizeBean.getSellerId());
			listunauthorizeappsBean.setSellerId(sellerId); 
			listunauthorizeappsCall.setData(listunauthorizeappsBean);
			listunauthorizeappsCall.callService();			
			
			Logger.verifyEquals("200", listunauthorizeappsCall.getCode(), "验证Code");
			
			List<Application> applicaton = listunauthorizeappsCall.getApplication().getContent();
			OpenApiWapper openApiWapper = new OpenApiWapper();
			List<com.ymttest.database.model.Application> applications = openApiWapper.selectUnauthorizeApplicationBySellerId(listunauthorizeappsBean.getSellerId());
			
			Logger.verifyEquals(applications.size(), applicaton.size(), "验证返回数据量");

			for(int i=0;i<applicaton.size();i++)
			{
				Logger.verifyEquals(applications.get(i).getApp_id(), applicaton.get(i).getAppId(), "验证返回appId与数据库一致");
				Logger.verifyEquals(applications.get(i).getApp_name(), applicaton.get(i).getAppName(), "验证返回appName与数据库一致");
				Logger.verifyEquals(applications.get(i).getType(), applicaton.get(i).getType(), "验证返回type与数据库一致");
				Logger.verifyEquals("THIRD_PARTY", applicaton.get(i).getType(), "验证返回type");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListUnauthorizeApps_002() {
		Logger.start(true,"查询所有未授权的第三方应用接口-sellerid不传");
		try {
			listunauthorizeappsCall.setData(listunauthorizeappsBean);
			listunauthorizeappsCall.callService();			
			
			Logger.verifyEquals("400", listunauthorizeappsCall.getCode(), "验证返回AppSecret与数据库一致");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListUnauthorizeApps_003() {
		Logger.start(true,"查询所有未授权的第三方应用接口-sellerid传0");
		try {
			listunauthorizeappsBean.setSellerId((long)0);
			listunauthorizeappsCall.setData(listunauthorizeappsBean);
			listunauthorizeappsCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListUnauthorizeApps_004() {
		Logger.start(true,"查询所有未授权的第三方应用接口-sellerid不存在");
		try {
			listunauthorizeappsBean.setSellerId((long)TestDataManager.getRandomIntNum(6));
			listunauthorizeappsCall.setData(listunauthorizeappsBean);
			listunauthorizeappsCall.callService();			
			
			List<Application> applicaton = listunauthorizeappsCall.getApplication().getContent();
			OpenApiWapper openApiWapper = new OpenApiWapper();
			List<com.ymttest.database.model.Application> applications = openApiWapper.selectUnauthorizeApplicationBySellerId(listunauthorizeappsBean.getSellerId());
			
			Logger.verifyEquals(applications.size(), applicaton.size(), "验证返回数据量");

			for(int i=0;i<applicaton.size();i++)
			{
				Logger.verifyEquals(applications.get(i).getApp_id(), applicaton.get(i).getAppId(), "验证返回appId与数据库一致");
				Logger.verifyEquals(applications.get(i).getApp_name(), applicaton.get(i).getAppName(), "验证返回appName与数据库一致");
				Logger.verifyEquals(applications.get(i).getType(), applicaton.get(i).getType(), "验证返回type与数据库一致");
				Logger.verifyEquals("THIRD_PARTY", applicaton.get(i).getType(), "验证返回type");
			}
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}